#dict字典
#键值映射
#无序
#可变数据类型
#key必须唯一

# dict1 = {}
# dict2 = dict()
#
# dict1 = {"a":1, 2:"3", None:"4","a":2}
# print(dict1)
# user_dict = {"root":"123456", "admin":"admin"}
# isp = {
#     "CMCC":"中国移动",
#     "CUCC":"中国联通"
# }

# 字典的定义
# • 字典的key必须是可hash对象 -- 不可变数据类型（list,dict等可变数据类型都不能作为key）
# • python中所有字符串、数字、元组、布尔值、None都是可hash对象
# • 字典的value可以是任何值


# dict1 = {"a":1,"b":0,"c":4,"d":3}
#查询
# print(dict1["a"])
# print(isp["CMCC"])

#print(dict1["f"])
#推荐使用这种方式获取，可以设置默认值，当找不到key的时候不会抛出异常
# print(dict1.get("f", 0))


#用法：
   # dicts[key]=value
   # 当key不存在，表示新增
   # 当key存在，表示修改
#修改
# dict1["a"] = 5
# print(dict1)

#新增
# dict1["e"] = 3
# print(dict1)

#删除
# 字典基本操作-删除
# • 用法1：dicts.pop(key)
# • 用法2：dicts.popitem()
#
# dict1.pop("a")
# print(dict1)
# dict1.popitem()
# print(dict1)

#python3.6之后对于dict的存储做了一些优化，按照什么顺序定义就按照什么顺序存储
#在python2版本，dict定义和存储的顺序是不一样的

#合并
# • 用法1：dict1.update(dict2) => 将dict2合并到dict1, 对自身进行操作
# 用法2：
# dict(dict1,**dict2) => 将dict1与dict2合并生成一个新字典

dict1 = {"a":1,"b":3,"c":1,"d":2}
dict2 = {"e":2,"f":4}
dict1.update(dict2)
print(dict1)

#pack,unpack
#**dict2  ==> e=2,f=4
# print(dict(dict1,**dict2))
# print(dict(dict1, e=2,f=4))
#
# user = {"name":"wy","age":18}
# ## **user == >name="wy",age=18
# print("my name is {name}, my age is {age}".format(name=user["name"],age=user["age"]))
# print("my name is {name}, my age is {age}".format(**user))

#成员关系
# >>> dict1 = {'a': 5, 'b': 0, 'c': 4, 'd': 3, 'e': 3}
# >>> dict1
# {'a': 5, 'b': 0, 'c': 4, 'd': 3, 'e': 3}
# >>> dict1
# {'a': 5, 'b': 0, 'c': 4, 'd': 3, 'e': 3}
# >>> "a" in dict1
# True
# >>> 0 in dict1
# False
# >>> 0 in dict1.values()
# True
# >>> dict1.values()
# dict_values([5, 0, 4, 3, 3])

#####遍历
# >>> dict1
# {'a': 5, 'b': 0, 'c': 4, 'd': 3, 'e': 3}
# >>> for i in dict1:
# ...    print(i)
# ...
# a
# b
# c
# d
# e
# >>> for i in dict1.keys():
# ...    print(i)
# ...
# a
# b
# c
# d
# e
# >>> for i in dict1.items():
# ...    print(i)
# ...
# ('a', 5)
# ('b', 0)
# ('c', 4)
# ('d', 3)
# ('e', 3)
# >>> for i,j in dict1.items():
# ...    print(i,"-->",j)
# ...
# a --> 5
# b --> 0
# c --> 4
# d --> 3
# e --> 3
# >>>




